# load packages
library(devtools)
library(popdictR)
library(lubridate)
library(quanteda)
library(tidyverse)
library(haven)

# Load dataset

# Install the dependency regexhelpeR from GitHub
devtools::install_github("jogrue/regexhelpeR")

# Install the multidictR package from GitHub
devtools::install_github("jogrue/multidictR")

# Install the popdictR package from GitHub
devtools::install_github("jogrue/popdictR")

# My dictionary
popdictR::gruendl_terms

# All terms (also available as .ods/.csv under /data-raw)
popdictR::gruendl_dictionary_complete



# Similar to what was done in the paper ----------------------------------------
# Form corpus of facebook data (fb=facebook)
fbcorp  = corpus(rename(table, text=v06, id=v01))


fbresult1 <- convert(run_popdict(fbcorp, at_level = "sentences",
                                 return_value = "binary",
                                 include_totals = TRUE),to = "data.frame")

df<-as_factor(fbresult1[c("v15a","v09_1","v09_2", "v09_3", "n_tokens", "n_sentences", "text", "dict_gruendl_2020")])

af<-as_factor(fbresult1[c("v15a","v09_1","v09_2", "v09_3","v09_8", "n_tokens", "n_sentences", "text", "dict_gruendl_2020")])


# Analysing the Hypotheses

# 1.) hypothesis: Especially populist parties use social media f.e. facebook for their party campaigns
# filter variables and analyse results

df %>%
  group_by(v15a) %>% 
  summarize(sum_popu = sum(dict_gruendl_2020>0),cnt=n(), popu_percent=round(sum_popu/cnt*100,1))

# results: 
# SP�: highest number of posts (682), only 3 posts are populist (0,4%)
# FP�: 555 posts, 54 are populist (9,7%)
# NEOS: 355 posts, 15 are populist (4,2%)
# conclusion
# 1. hypotheses can not be verified, because other Austrian parties also use facebook frequently
# but obviously FP� and NEOS have the highest share of populist posts


# 2.) hypothesis: the more populist a post, the higher the virality 

# likes (v09_1), comments (v09_2) and shares(v09_3) are the dependent variables
# the number of populist sentences (dict_gruendl_2020) is the independent variable
# both variables are metric
# analyse the correlation for every party

# regression with interaction term 
reg_af <- lm(v09_8~ dict_gruendl_2020*v15a,data = af)

summary(reg_af)
#likes
reg_1 <- lm(v09_1 ~ dict_gruendl_2020*v15a,
            data = df)
#comments
reg_2 <- lm(v09_2 ~ dict_gruendl_2020*v15a,
            data = df)

#shares
reg_3 <- lm(v09_3~ dict_gruendl_2020*v15a,
            data = df)

# more packages needed 
install.packages("broom.mixed")
install.packages("ggstance")
install.packages("jtools")

#Package for Plotting
library(jtools)

#plot effects
plot_summs(reg_1) 

#result ?

### Text analysis per party ######

sp<-filter(df,v15a=="Social Democratic Party of Austria (SP�)")
vp<-filter(df,v15a=="Austrian People's Party (�VP)")
fp<-filter(df,v15a=="Freedom Party of Austria (FP�)")
gr<-filter(df,v15a=="Austrian Green Party (Greens)")
neos<-filter(df,v15a=="NEOS - The New Austria and Liberal Forum")
jz<-filter(df,v15a=="JETZT - Pilz List")

###regressions per party###

##SP�##
reg_1.1<-lm(v09_1 ~ dict_gruendl_2020, data=sp)

##�VP##
reg_1.2<-lm(v09_1 ~ dict_gruendl_2020, data=vp)

##FP�##
reg_1.3<-lm(v09_1 ~ dict_gruendl_2020, data=fp)

##Gr�ne##
reg_1.4<-lm(v09_1 ~ dict_gruendl_2020, data=gr)

##NEOS##
reg_1.5<-lm(v09_1 ~ dict_gruendl_2020, data=neos)

##Pilz/Jetzt##
reg_1.6<-lm(v09_1 ~ dict_gruendl_2020, data=jz)


####Results of each regression###
#SP�#
summary(reg_1.1)

#�VP#
summary(reg_1.2)

#FP�#
summary(reg_1.3)

#Gr�ne#
summary(reg_1.4)

#Neos#
summary(reg_1.5)

#Pilz/Jetzt#
summary(reg_1.6)


#### Plot for the regression of all parties 

plot_summs(reg_1.1,reg_1.2,reg_1.3,reg_1.4,reg_1.5,reg_1.5,reg_1.6,model.names = c("SPÖ","ÖVP","FPÖ","Greens","NEOS","PILZ/JETZT"),colors = c("red","turquoise","royalblue","seagreen","orchid2","slategrey"),legend.title = "Party")


# first try correlation/ without interaction terms (not as significant)
# likes
cor(df$dict_gruendl_2020, df$v09_1, use = "complete.obs")
cor.test(df$v09_1, df$dict_gruendl_2020, use = "complete.obs" )

# comments
cor(df$v09_2, df$dict_gruendl_2020, use = "complete.obs")
cor.test(df$v09_2, df$dict_gruendl_2020, use = "complete.obs" )

# shares 
cor(df$v09_3, df$dict_gruendl_2020, use = "complete.obs")
cor.test(df$v09_3, df$dict_gruendl_2020, use = "complete.obs" )
 
# result: correlations are < 0,1 -> weak correlations 
# p-value for shares: 0.0002877 < 0,05 
# conlusion: no siginificant correlation, but small effect visible
# 2. hypothesis can not be verified

# graphic for shares (v09_3)
plot(df$dict_gruendl_2020, df$v09_3)


#### Regression for reaction "angry" and correlation ####

af<-as_factor(fbresult1[c("v15a","v09_1","v09_2", "v09_3","v09_8", "n_tokens", "n_sentences", "text", "dict_gruendl_2020")])

reg_af <- lm(v09_8~ dict_gruendl_2020*v15a,data = af)
summary(reg_af)

cor(af$dict_gruendl_2020, af$v09_8, use = "complete.obs")
cor.test(af$v09_8, af$dict_gruendl_2020, use = "complete.obs" )

#### Dataset with all interactions from v09_1 to v09_8####

rf<-as_factor(fbresult1[c("v15a","v09_1","v09_2", "v09_3","v09_4","v09_5","v09_6","v09_7","v09_8", "n_tokens", "n_sentences", "text", "dict_gruendl_2020")])



#### adding up all interactions
rf$interaction_all <- rowSums(rf[, 2:9])


sp<-filter(rf,v15a=="Social Democratic Party of Austria (SP�)")
vp<-filter(rf,v15a=="Austrian People's Party (�VP)")
fp<-filter(rf,v15a=="Freedom Party of Austria (FP�)")
gr<-filter(rf,v15a=="Austrian Green Party (Greens)")
neos<-filter(rf,v15a=="NEOS - The New Austria and Liberal Forum")
jz<-filter(rf,v15a=="Pilz/Jetzt")

###regressions per party###

##SP�##
reg_2.1<-lm(v09_8 ~ dict_gruendl_2020, data=sp)

##�VP##
reg_2.2<-lm(v09_8 ~ dict_gruendl_2020, data=vp)

##FP�##
reg_2.3<-lm(v09_8 ~ dict_gruendl_2020, data=fp)

##Gr�ne##
reg_2.4<-lm(v09_8 ~ dict_gruendl_2020, data=gr)

##NEOS##
reg_2.5<-lm(v09_8 ~ dict_gruendl_2020, data=neos)

##Pilz/Jetzt##
reg_2.6<-lm(v09_8 ~ dict_gruendl_2020, data=jz)

###results of each regression###
#SP�#
summary(reg_2.1)
#�VP#
summary(reg_2.2)
#FP�#
summary(reg_2.3)
#Gr�ne#
summary(reg_2.4)
#Neos#
summary(reg_2.5)
#Pilz/Jetzt#
summary(reg_2.6)

#### Plot for the regression of all parties 

plot_summs(reg_2.1,reg_2.2,reg_2.3,reg_2.4,reg_2.5,reg_2.5,reg_2.6,model.names = c("SPÖ","ÖVP","FPÖ","Greens","NEOS","PILZ/JETZT"),colors = c("red","turquoise","royalblue","seagreen","orchid2","slategrey"),legend.title = "Party")


###Regression for all interactions####
reg_2.1<-lm(interaction_all ~ dict_gruendl_2020, data=sp)

##�VP##
reg_2.2<-lm(interaction_all ~ dict_gruendl_2020, data=vp)

##FP�##
reg_2.3<-lm(interaction_all ~ dict_gruendl_2020, data=fp)

##Gr�ne##
reg_2.4<-lm(interaction_all ~ dict_gruendl_2020, data=gr)

##Neos##
reg_2.5<-lm(interaction_all ~ dict_gruendl_2020, data=neos)

##Pilz/Jetzt##
reg_2.6<-lm(interaction_all ~ dict_gruendl_2020, data=jz)

####results of each regression###
#SP�#
summary(reg_2.1)

#�VP#
summary(reg_2.2)

#FP�#
summary(reg_2.3)

#Gr�ne#
summary(reg_2.4)

#Neos#
summary(reg_2.5)

#Pilz/Jetzt#
summary(reg_2.6)


plot_summs(reg_2.1,reg_2.2,reg_2.3,reg_2.4,reg_2.5,reg_2.5,reg_2.6,model.names = c("SPÖ","ÖVP","FPÖ","Greens","NEOS","PILZ/JETZT"),colors = c("red","turquoise","royalblue","seagreen","orchid2","slategrey"),legend.title = "Party")
